package cz.cernilovsky.android.profileapp.services;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.json.JSONException;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationStatusCodes;
import com.google.android.gms.location.LocationClient.OnAddGeofencesResultListener;

import cz.cernilovsky.android.profileapp.R;
import cz.cernilovsky.android.profileapp.activities.MainActivity;
import cz.cernilovsky.android.profileapp.model.Location;
import cz.cernilovsky.android.profileapp.storage.LocationsStorage;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.util.Log;

public class MonitorGeofencesService extends Service {
	private static final String TAG = "MonitorGeofencesService";
	private static boolean started = false;
	
	@Override
	public IBinder onBind(Intent intent) {
		//connect();
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		//connect();
		return START_STICKY;
		//return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		started = true;
		//Log.d(TAG, "running");
	}

	private void quit() {
		RemoveGeofence.removeAllLocations(getApplicationContext());
		NotificationService.notify(getApplicationContext(), R.string.service_destroyed, R.string.error);
		NotificationManager nm =
			    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		nm.cancel(0);
	}

	@Override
	public void onDestroy() {
		quit();
		Log.i(TAG, "Destroyed");
		started = false;
		super.onDestroy();
	}
	
	public static boolean isStarted() {
		return started;
	}
	
}